package com.airbnb.mvrx.sample.features.clickread.views

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import android.widget.TextView
import com.airbnb.epoxy.TypedEpoxyController
import com.example.mvrxstudy.R
import com.yt.ytdeep.client.dto.ClickReadPage

class CRBottomView : FrameLayout {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private val mPage: TextView
    private val mCarousel: SnappingRecyclerView
    private val mController: PageThumbController

    init {
        inflate(context, R.layout.view_bottom_cr, this)
        mPage = findViewById(R.id.tv_page)
        mCarousel = findViewById(R.id.v_carousel)
        mController = PageThumbController()
        mCarousel.setController(mController)
    }

    fun setThumbs(thumbs: List<ClickReadPage>) {
        mController.setData(thumbs)
    }

    fun setPosition(position: Int) {
        mCarousel.scrollToPosition(position)
    }

    fun setOnViewSelectedListener(listener: SnappingRecyclerView.OnViewSelectedListener?) {
        mCarousel.setOnViewSelectedListener(listener)
    }

    class PageThumbController : TypedEpoxyController<List<ClickReadPage>>() {
        override fun buildModels(data: List<ClickReadPage>?) {
            data!!.forEachIndexed { index, each ->
                cRThumbItem {
                    id(each.id)
                    image(each.thumbnails)
                    position(index)
                }
            }
        }

    }

}